/**
 * 返回一个table dom
 * @param title
 * @param style
 * @param relation [{"relationId":"ca201d6613ee11e9a82bfa163eb05f5d","relationDimension":"legend","relationColumn":"b37b3c8613ee11e9a82bfa163eb05f5d","relationReport":"ca1d427a13ee11e9a82bfa163eb05f5d","resultColumn":{"columnId":"b37b3c8613ee11e9a82bfa163eb05f5d","columnDesc":"ip","columnField":"ip","columnDataType":"VARCHAR","columnQuery":"b377fede13ee11e9a82bfa163eb05f5d"}}]
 * @param data
 * @returns {Array}
 */

function template(title, style, relations, data) {


    if (!Array.isArray(relations) || !Array.isArray(data)) {

        console.info("数据格式错误");

        return [];
    }


    let xKey = '';

    let yKey = '';

    let dataKeys = [];


    let dimensionMap = {};

    let xDimensions=[];

    let yDimensions=[];



    for (let relation of relations) {

        let field = relation['resultColumn']['columnField'];

        let name = relation['resultColumn']['columnDesc'];

        if (relation['relationDimension'] == 'x') {

            xKey = field;

        } else if (relation['relationDimension'] == 'y') {

            yKey = field;
        } else {

            dataKeys.push(field);
        }

        dimensionMap[field] = name;

    }


    if (xKey == '' || yKey == '' || dataKeys.length == 0) {

        console.info("关系格式错误");

        return [];
    }


    //处理y轴维度

    let yDimension = {};

    for (let row of data) {


        let yData = row[yKey];

        if (yDimension[yData] == undefined) {

            //y轴维度不包含当前数据，添加进去

            let dimensionKeys = Object.keys(yDimension);

            if(dimensionKeys.length > 0){

                //从其他y轴维度复制x维度
                yDimension[yData] = Object.assign({},yDimension[dimensionKeys[0]]);

                //数值置0

                for(let yk in yDimension[yData]){

                    for(let xk in yDimension[yData][yk]){

                        yDimension[yData][yk][xk] = 0;

                    }
                }



            }else{

                yDimension[yData]={};

            }

            yDimensions.push(yData);


        }

        let xData = row[xKey];


        if(xDimensions.indexOf(xData) < 0){

            xDimensions.push(xData);
        }


        let dataObjs = yDimension[yData][xData];

        if (dataObjs == undefined) {

            dataObjs = {}

            for (let dataKey of dataKeys) {

                dataObjs[dataKey] = row[dataKey];
            }


            yDimension[yData][xData] = dataObjs;

        } else {

            //合并X轴数据

            for (let dataKey of dataKeys) {

                let oldData = dataObjs[dataKey];

                let newData = row[dataKey];

                dataObjs[dataKey] = oldData + newData;

            }

        }


        //其他X维度不存在，造一个

        for(let k in yDimension){
            if(k != yData){


                let dataObjs = yDimension[k][xData];

                if (dataObjs == undefined) {

                    dataObjs = {}

                    for (let dataKey of dataKeys) {

                        dataObjs[dataKey] = 0;
                    }


                    yDimension[k][xData] = dataObjs;

                }else{

                    for (let dataKey of dataKeys) {

                        let oldData = dataObjs[dataKey];

                        if(oldData == undefined){

                            dataObjs[dataKey] = 0;

                        }

                    }
                }


            }
        }


    }



    //生成一个x轴统计key ，避免与x轴其他维度重复
    let xTotalKey = (new Date()).valueOf();

    let html = "<table>";

    html += "<tr>";

    html += "<td rowspan='2'>";

    html += dimensionMap[xKey];

    html += "</td>";

    xDimensions.push("总计");

    for(let obj of xDimensions){

        html += "<td colspan='"+ dataKeys.length +"'>";
        html += obj;
        html += "</td>";
    }

    html += "</tr>";

    //第二行

    html += "<tr>";

    for(let i = 0 ; i < xDimensions.length;i++){

        for(let d of dataKeys){


            html += "<td>";
            html += dimensionMap[d];
            html += "</td>"
        }
    }


    html += "</tr>";

    //数据

    yDimension["总计"] = {};

    for(let ydItem in yDimension){

        html += "<tr>"

        //第一列

        html += "<td>" + ydItem + "</td>";

        let datas = yDimension[ydItem];




        let xSum = {};

        datas["总计"] = xSum;


        //渲染X轴维度
        for(let obj of xDimensions){

            let columns = datas[obj];

            if(yDimension["总计"][obj] == undefined){

                yDimension["总计"][obj] = {};
            }

            //渲染数据维度
            for(let d of dataKeys) {


                html += "<td>";
                html += columns[d];
                html += "</td>"



                if(yDimension["总计"][obj][d] == undefined){
                    yDimension["总计"][obj][d] = 0;
                }

                if(xSum[d] == undefined){
                    xSum[d] = 0;
                }

                if(obj != "总计"){

                    xSum[d] += columns[d];
                }

                if(ydItem != "总计"){

                    yDimension["总计"][obj][d] += columns[d];
                }

            }

        }






        html+= "</tr>"

    }


    html += "</table>";


    return html;

}